﻿using System;
using System.Text;
using System.Collections.Generic;
using System.Linq;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Cesave.Domain.PermisosModule.Contracts.Areas;
using Cesave.Infrastructure.CrossCutting.IoC;
using Cesave.Domain.PermisosModule.Entities;

namespace Cesave.Domain.PermisosModule.Tests.ServicesTests
{
	[TestClass]
	[DeploymentItem("Cesave.Infrastructure.Data.PermisosModule.Mock.dll")]
	[DeploymentItem("Cesave.Infrastructure.Data.PermisosModule.dll")]
	public class AreaServiceTests
	{

		[TestMethod]
		[ExpectedException(typeof(ArgumentNullException))]
		public void AddArea_Null_Area_Throws_ArgumentNullException()
		{
			// Arrange
			IAreaService areaService = IoCFactory.Resolve<IAreaService>();

			// Act
			areaService.AddArea(null);
		}

		[TestMethod]
		public void AddArea_Invoke()
		{
			// Arrange
			IAreaService service = IoCFactory.Resolve<IAreaService>();

			var guid = Guid.NewGuid();

			var expected = new Area
			{
				AreaGuid = guid,
				AreaId = 99,
				JuntaGuid = Guid.NewGuid(),
				JuntaId = 2,
				MostrarEnExpedicion = true,
				Status = true,
				Letra = "T",
				Nombre = "Test",
			};
			// Act
			service.AddArea(expected);

			var actual = service.FindAreaByGuid(guid);

			// Assert
			Assert.AreEqual(guid, actual.AreaGuid);
			Assert.AreEqual("T", actual.Letra);

			// clean
			service.DeleteArea(expected);
			Assert.IsNull(service.FindAreaByGuid(guid));
		}

		[TestMethod]
		[ExpectedException(typeof(ArgumentNullException))]
		public void ChangeArea_Null_Area_Throws_ArgumentNullException()
		{
			// Arrange
			IAreaService service = IoCFactory.Resolve<IAreaService>();

			// Act
			service.ChangeArea(null);
		}

		[TestMethod]
		public void ChangeArea_Invoke()
		{
			// Arrange
			IAreaService service = IoCFactory.Resolve<IAreaService>();

			Guid guid = Guid.NewGuid();
			var newArea = new Area
			{
				AreaGuid = guid,
				AreaId = 99,
				JuntaGuid = Guid.NewGuid(),
				JuntaId = 2,
				Letra = "T",
				MostrarEnExpedicion = true,
				Nombre = "Test",
				Status = true,
			};

			service.AddArea(newArea);

			var expected = service.FindAreaByGuid(guid);

			// Act
			expected.Letra = "F";
			service.ChangeArea(expected);

			var actual = service.FindAreaByGuid(guid);
			// Assert
			Assert.IsNotNull(actual);
			Assert.AreEqual("F", actual.Letra);

			// clean
			service.DeleteArea(actual);
			Assert.IsNull(service.FindAreaByGuid(guid));
		}

		[TestMethod]
		[ExpectedException(typeof(ArgumentException))]
		public void FindAreasByJunta_Empty_Guid_Throws_ArgumentException()
		{
			// Arrange
			IAreaService service = IoCFactory.Resolve<IAreaService>();

			// Act
			var actual = service.FindAreasByJunta(Guid.Empty);
		}

		[TestMethod]
		public void FindAreasByJunta_Invoke()
		{
			// Arrange
			IAreaService service = IoCFactory.Resolve<IAreaService>();

			Guid guid = new Guid("C9760433-39E5-4FE4-A17E-EDE111734C65");

			// Act
			var actual = service.FindAreasByJunta(guid);

			// Assert
			Assert.IsNotNull(actual);
			Assert.AreEqual(3, actual.Count());
		}
	}
}